package v1

import (
	"api-go/cache"
	"api-go/model"
	"api-go/serializer"
	v1 "api-go/service/v1/user"
	"fmt"

	"github.com/gin-gonic/gin"
)

// 用户注册
func UserRegister(c *gin.Context) {
	var service v1.UserRegisterService
	var res *serializer.Response
	if err := c.ShouldBind(&service); err != nil {
		fmt.Println(err)
		res = serializer.ErrorResponse(serializer.CodeParamError)
	} else {
		res = service.Register(c)
	}
	c.JSON(200, res)
}

// 用户登录
func UserLogin(c *gin.Context) {
	var service v1.UserLoginService
	var res *serializer.Response
	if err := c.ShouldBind(&service); err != nil {
		res = serializer.ErrorResponse(serializer.CodeParamError)
	} else {
		res = service.Login()
	}
	c.JSON(200, res)
}

// 查看个人信息
func UserMe(c *gin.Context) {
	var res *serializer.Response
	if user := CurrentUser(c); user != nil {
		res = serializer.OkResponse(serializer.BuildUserResponse(user))
	} else {
		res = serializer.ErrorResponse(serializer.CodeUnknownError)
	}
	c.JSON(200, res)
}

// 退出登录
func UserLogout(c *gin.Context) {
	token, _ := c.Get("token")
	tokenString := token.(string)

	cache.RedisClient.SAdd(cache.Context, "jwt:baned", tokenString)
	c.JSON(200, serializer.OkResponse(nil))
}

// 获取当前用户
func CurrentUser(c *gin.Context) *model.User {
	if userID, ok := c.Get("user_id"); ok {
		if user, err := model.GetUser(*userID.(*uint)); err == nil {
			return user
		}
	}
	return nil
}

//根据邮箱修改密码
func ChangePasswordByEmail(c *gin.Context) {
	var service v1.ChangePassService
	var res *serializer.Response
	if err := c.ShouldBind(&service); err != nil {
		res = serializer.ErrorResponse(serializer.CodeParamError)
	} else {
		res = service.ChangePasswordByEmail()
	}
	c.JSON(200, res)
}

// 用户文件上传
func UserFileUpload(c *gin.Context) {
	res := v1.FileUpload(c)
	c.JSON(200, res)
}

//修改用户信息
func UpdateUserInfo(c *gin.Context) {
	var service v1.UserUpdateService
	var res *serializer.Response
	if user := CurrentUser(c); user != nil {
		if err := c.ShouldBind(&service); err != nil {
			fmt.Println(err)
			res = serializer.ErrorResponse(serializer.CodeParamError)
		} else {
			res = service.UpdateUserInfo(user)
		}
	} else {
		res = serializer.ErrorResponse(serializer.CodeUnknownError)
	}
	c.JSON(200, res)
}

//上传喜欢的歌单
func InsertLikePlaylist(c *gin.Context) {
	var service v1.Like
	var res *serializer.Response
	if userID, ok := c.Get("user_id"); ok {
		if err := c.ShouldBind(&service); err != nil {
			fmt.Println(err)
			res = serializer.ErrorResponse(serializer.CodeParamError)
		} else {
			res = service.InsertLikePlaylist(*userID.(*uint))
		}
	} else {
		res = serializer.ErrorResponse(serializer.CodeUnknownError)
	}
	c.JSON(200, res)
}

//上传喜欢的歌曲
func InsertLikeSong(c *gin.Context) {
	var service v1.Like
	var res *serializer.Response
	if userID, ok := c.Get("user_id"); ok {
		if err := c.ShouldBind(&service); err != nil {
			fmt.Println(err)
			res = serializer.ErrorResponse(serializer.CodeParamError)
		} else {
			res = service.InsertLikeSong(*userID.(*uint))
		}
	} else {
		res = serializer.ErrorResponse(serializer.CodeUnknownError)
	}
	c.JSON(200, res)
}

//获取喜欢的歌单
func GetLikePlaylist(c *gin.Context) {
	var service v1.Like
	var res *serializer.Response
	if userID, ok := c.Get("user_id"); ok {
		res = service.GetPlaylistByUserId(*userID.(*uint))
	} else {
		res = serializer.ErrorResponse(serializer.CodeUnknownError)
	}
	c.JSON(200, res)
}

//获取喜欢的歌曲
func GetLikeSong(c *gin.Context) {
	var service v1.Like
	var res *serializer.Response
	if userID, ok := c.Get("user_id"); ok {
		res = service.GetSongByUserId(*userID.(*uint))
	} else {
		res = serializer.ErrorResponse(serializer.CodeUnknownError)
	}
	c.JSON(200, res)
}

//删除全部歌单
func DeleteAllPlaylist(c *gin.Context) {
	var service v1.Like
	var res *serializer.Response
	if userID, ok := c.Get("user_id"); ok {
		res = service.DeleteAllPlaylist(*userID.(*uint))
	} else {
		res = serializer.ErrorResponse(serializer.CodeUnknownError)
	}
	c.JSON(200, res)
}

//删除全部歌曲
func DeleteAllSong(c *gin.Context) {
	var service v1.Like
	var res *serializer.Response
	if userID, ok := c.Get("user_id"); ok {
		res = service.DeleteAllSong(*userID.(*uint))
	} else {
		res = serializer.ErrorResponse(serializer.CodeUnknownError)
	}
	c.JSON(200, res)
}

//上传喜欢的歌单和音乐
func InsertLikeList(c *gin.Context) {
	var service v1.Like
	var res *serializer.Response
	if userID, ok := c.Get("user_id"); ok {
		if err := c.ShouldBind(&service); err != nil {
			fmt.Println(err)
			res = serializer.ErrorResponse(serializer.CodeParamError)
		} else {
			res = service.InsertLikeList(*userID.(*uint))
		}
	} else {
		res = serializer.ErrorResponse(serializer.CodeUnknownError)
	}
	c.JSON(200, res)
}

//获取喜欢的歌单和音乐
func GetLikeList(c *gin.Context) {
	var service v1.Like
	var res *serializer.Response
	if userID, ok := c.Get("user_id"); ok {
		res = service.GetLikeList(*userID.(*uint))
	} else {
		res = serializer.ErrorResponse(serializer.CodeUnknownError)
	}
	c.JSON(200, res)
}

//用户签到
func Daka(c *gin.Context) {
	var service v1.DakaTime
	var res *serializer.Response
	if userID, ok := c.Get("user_id"); ok {
		if err := c.ShouldBind(&service); err != nil {
			res = serializer.ErrorResponse(serializer.CodeParamError)
		} else {
			res = service.DakaToday(*userID.(*uint))
		}
	} else {
		res = serializer.ErrorResponse(serializer.CodeUnknownError)
	}
	c.JSON(200, res)
}

//获取用户连续签到天数以及总积分
func DakaInfo(c *gin.Context) {
	var service v1.DakaTime
	var res *serializer.Response
	if userID, ok := c.Get("user_id"); ok {
		res = service.DakaInfo(*userID.(*uint))
	} else {
		res = serializer.ErrorResponse(serializer.CodeUnknownError)
	}
	c.JSON(200, res)
}

//用户根据年月获取打卡信息
func DakaInfoByMonth(c *gin.Context) {
	var service v1.DakaTime
	var res *serializer.Response
	if userID, ok := c.Get("user_id"); ok {
		if err := c.ShouldBind(&service); err != nil {
			res = serializer.ErrorResponse(serializer.CodeParamError)
		} else {
			res = service.DakaInfoByMonth(*userID.(*uint))
		}
	} else {
		res = serializer.ErrorResponse(serializer.CodeUnknownError)
	}
	c.JSON(200, res)
}

//判断今天是否打卡
func IsDakaToday(c *gin.Context) {
	var service v1.DakaTime
	var res *serializer.Response
	if userID, ok := c.Get("user_id"); ok {
		res = service.IsDakaToday(*userID.(*uint))
	} else {
		res = serializer.ErrorResponse(serializer.CodeUnknownError)
	}
	c.JSON(200, res)
}

//获取无损格式歌曲信息
func UserSongFlac(c *gin.Context) {
	var service v1.Song
	var res *serializer.Response
	if userID, ok := c.Get("user_id"); ok {
		if err := c.ShouldBind(&service); err != nil {
			res = serializer.ErrorResponse(serializer.CodeParamError)
		} else {
			res = service.SongFlac(*userID.(*uint))
		}
	} else {
		res = serializer.ErrorResponse(serializer.CodeUnknownError)
	}
	c.JSON(200, res)
}
